<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class info extends CI_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	
    function __construct() {
        parent::__construct();
        $this->load->model("muser");
        $this->load->model("minfo");
        $this->load->model("minfocate");
        $this->load->model("mresource");
		
		$this->resCode = $this->minfo->resCode;
        $this->url_index = admin_url() . "info";
        $this->folder_upload = "dataupload/info/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
    }

    public function index() {
        $data = array();
        $data['datas'] = $this->minfo->gets();
        $data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $this->theme_data["main"] = "info/index";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    function edit($infoID = 0) {
        $data = array();
        $post = $this->input->post();
        $data['info'] = $this->minfo->get($infoID, 'INF_ID');
		$data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $data['error'] = array('err_code' => 0, 'msg' => '');
        if ($post) {
			if($upload = $this->upload(@$data['info']->INF_THUMB)){
				$post['info']['INF_THUMB'] = $this->folder_upload.$upload['upload_data']['file_name'];
			}
            if ($infoID > 0) {
                $this->minfo->update($infoID, 'INF_ID', $post['info']);
            }
			else{
				$infoID = $this->minfo->insert($post['info']);
			}
            redirect($this->url_edit . $infoID);
        }
		$infocate = $this->minfocate->gets();
		$data['infocate'] = array();
		foreach($infocate as $r){
			$data['infocate'][$r->INFCATE_ID] =$r->INFCATE_TITLE;
		}
		$data['images'] = $this->minfo->getImageDetail($infoID);
        $this->theme_data["main"] = "info/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function delete($infoID = 0){
		if($infoID > 0){
			$info = $this->minfo->get($infoID, 'INF_ID');
			if(file_exists('./'.$info->INF_THUMB))
				unlink('./'.$info->INF_THUMB);
			$this->minfo->delete($infoID, 'INF_ID');
		}
	}
	
	function upload($old_name = '') {
		$config['upload_path'] = './'.$this->folder_upload;
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		
		//$config['max_size']	= '10000';
		//$config['max_width']  = '1024';
		//$config['max_height']  = '768';
		if(!is_dir($this->folder_upload))
			mkdir($this->folder_upload,0777);
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload())
		{
			return false;
		}
		else
		{
			if($old_name && isset($old_name))
			unlink('./'.$old_name);
			return array('upload_data' => $this->upload->data());
		}
	}
	
	function do_upload_products_image() {
        
        $infoID = $this->input->post('param1');
		 if(!$infoID || $infoID <0 || $infoID ==''){
			return false;
		 }
		 $info = $this->minfo->get($infoID,'INF_ID');
		 if(!$info){
			 return false;
		 }
			$config['upload_path'] = './'.$this->folder_upload.'images/';
			$config['allowed_types'] = 'gif|jpg|jpeg|png';
			$config['file_name'] = $info->INF_CODE.'-'.$info->INF_ID;
			$this->load->library('upload', $config);
			if(!is_dir($this->folder_upload))
				mkdir($this->folder_upload,0777);
			if(!is_dir($config['upload_path']))
				mkdir($config['upload_path'],0777);
			if (!$this->upload->do_upload('Filedata'))
			{
				echo $this->upload->display_errors('<p>', '</p>');

				return false;
			}
			else
			{
				$newFile = $this->upload->data();
			}
				 $array=array(
				 	"INF_ID"=>$infoID,
					"INF_IP"=>$this->input->ip_address(),
					"INF_TIME_CREATE"=>time(),
				 	"INF_PATCH"=>$this->folder_upload.'images/' . $newFile['file_name']
				 );
				  $this->db->insert("info_images",$array);
           $newID = $this->db->insert_id();
               		 echo json_encode(array('id'=>$newID, 'link'=>base_url().$this->folder_upload.'images/' . $newFile['file_name']));
                /* end write to database */
    }
	
	function deleteimage(){
		$infoImageID = $this->input->post("id");
		if($infoImageID >0){
			$check = $this->db->from("info_images")->where("INFI_ID",$infoImageID)->get()->first_row();
			if($check){
				if($check->INF_PATCH && file_exists('./'.$check->INF_PATCH))
					unlink('./'.$check->INF_PATCH);
				$this->db->where("INFI_ID",$infoImageID);
				$this->db->delete("info_images");
			}
		}
	}
	function getimagelist(){
		$infoImageID = $this->input->post("id");
		$data = array();
		if($infoImageID && $infoImageID>0){
			$this->db->where("INF_ID",$infoImageID);
			$images = $this->db->from("info_images")->get()->result();
			 
			if($images){
			foreach($images as $image){
				$data[] = array(
					'id'=>$image->INFI_ID,
					'url'=>base_url().$image->INF_PATCH,
				);
				}
			}
		}
		die(json_encode($data));
	}
}
